<?php

defined('__MAPEXEC__') or die('No direct access allowed');

/**
 * Exports map data as an associative array, usefull for programmers
 * more comfortable with procedural than object oriented code.
 */
class Maps_Export_Raw extends Maps_Export_Common {
  
  /**
   * Renders the map and readies it for export.
   * @param   Maps_Map_Common           The map object to export
   * @param   Maps_Icon_Manager_Common  Optional icon manager object
   * @return  Array                     All the map information in array format.
   */
  public function render(Maps_Map_Common $map, Maps_Icon_Manager_Common $iconManager = null) {
    $raw = array();
    
    if ( !isset($map) ) {
      throw new Exception('$map is null and cannot be exported.');
      return null;
    }
    
    $raw['layer_controls'] = $map->getLayerControls();
    $raw['lines'] = $this->renderAllLines($map);
    $raw['map_center'] = array('lat'=>$map->getCenterLat(), 'lng'=>$map->getCenterLng());
    $raw['mapId'] = $map->getId();
    $raw['navigation'] = $map->getNavigationControls();
    $raw['poiCount'] = $map->getPoiCount();
    $raw['pois'] = $this->renderAllPois($map);
    $raw['polygons'] = $this->renderAllPolygons($map);

    return $raw;
  }
  
  /**
   * Renders all map lines
   * @param   Maps_Map_Common   Map object containing lines
   * @return  Array             Array of lines info
   */
  protected function renderAllLines(Maps_Map_Common $map) {
    $line = null;
    $lines = array();
    
    while ($line = $map->lines->next()) {
      $lines[] = $this->renderLine($line);
    }
    
    return $lines;
  }
  
  /**
   * Renders all Points Of Interest
   * @param   Array   List of all the POI objects in a map.
   * @return  Array   The POI objects information in array form
   */
  protected function renderAllPois(Maps_Map_Common $map) {
    $raw = array();
    while ($poi = $map->pois->next()) {
      $raw[] = $this->renderPoi($poi);
    }
    return $raw;
  }
  
  /**
   * Renders all the map polygons
   * @param   Maps_Map_Common   Map object containing polygons
   * @return  Array             Array of polygons info
   */
  protected function renderAllPolygons(Maps_Map_Common $map) {
    $polygon = null;
    $polygonInfo = array();
    
    while ($polygon = $map->polygons->next()) {
      $polygonInfo[] = $this->renderPolygon($polygon);
    }
    
    return $polygonInfo;
  }
  
  /**
   * Renders a lat/lng object
   * @param   Maps_Latlng   Latitude/longitude object
   * @return  Array         Associative array containing lat and lng items.
   */
  protected function renderLatLng(Maps_LatLng $latlng) {
    return array('lat'=>$latlng->getLat(), 'lng'=>$latlng->getLng());
  }
  
  /**
   * Renders a line object
   * @param   Maps_Map_Polyline   Line object
   * @return  Array               Array of line info
   */
  protected function renderLine(Maps_Map_Polyline $line) {
    return $this->renderLatLngCollection($line);
  }
  
  /**
   * Renders a collection of geographic points, as in a line or polygon.
   * @param   mixed   Maps_Map_Polyline or Maps_Map_Polygon object
   * @return  Array   Info about the geographic point collection.
   */
  private function renderLatLngCollection($collection) {
    $info = array(
      'latlngs' => array(),
      'id' => ''
    );
    $latlng = null;
    
    while ($latlng = $collection->next()) {
      $info['latlngs'][] = $this->renderLatLng($latlng);
    }
    
    $info['id'] = $collection->getId();
    
    return $info;
  }
  
  /**
   * Renders an individual POI for export.
   * @param MapPoi $poi Point Of Interest object
   * @return array POI object information in array form
   */
  protected function renderPoi(Maps_Map_POI $poi) {
    $raw = array();
    $raw['latlng'] = $this->renderLatLng( $poi->getLatLng() );
    $raw['title'] = $this->escape( $poi->getTitle(), 'html' );
    $raw['name'] = $this->escape( $poi->getName(), 'html' );
    $raw['description'] = $this->escape( $poi->getDescription(), 'html' );
    $raw['address'] = $this->escape( $poi->getAddress(), 'html' );
    $raw['contacts'] = $this->escape( $poi->getContacts(), 'html' );
    $raw['website'] = $this->escape( $poi->getWebsite(), 'html' );
    $raw['url'] = $this->escape( $poi->getURL(), 'html' );
    $raw['image'] = $this->escape( $poi->getImage(), 'html' );
    return $raw;
  }
  
  /**
   * Renders a polygon object.
   * @param   Maps_Map_Polygon  Polygon object
   * @return  Array             Array of polygon info
   */
  protected function renderPolygon(Maps_Map_Polygon $polygon) {
    return $this->renderLatLngCollection($polygon);
  }
  
}
?>